package com.wan37.logic.backpack.weaponry.service.impl;

import com.wan37.logic.backpack.weaponry.service.WeaponryService;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.UserWeaponryDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.service.UserWeaponryService;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.service.cache.WeaponryServiceCache;
import com.wan37.logic.utils.BuffUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-07-21 16:20
 **/
@Component("weaponryService")
public class WeaponryServiceImpl implements WeaponryService {
    private final Logger logger = LoggerFactory.getLogger(WeaponryServiceImpl.class);
    @Autowired
    private RouteUtils routeUtils;
    @Autowired
    private BuffUtil buffUtil;
    @Autowired
    private WeaponryServiceCache weaponryServiceCache;
    @Autowired
    private AsynDBManager asynDBManager;
    @Autowired
    private UserWeaponryService userWeaponryService;
    @Autowired
    private SceneServiceCache sceneServiceCache;

    @Override
    public void reduceDurability(WeaponryVO weaponryVO, MessageVO messageVO) {
        int durability = weaponryVO.getDurability();
        if (durability > 0) {
            weaponryVO.setDurability(durability - 1);
        } else {

        }
    }

    /**
     * 装配装备
     *
     * @param weaponryVO
     * @param user
     * @param role
     * @param messageVO
     */
    @Override
    public void updateWeaponryUsageUse(WeaponryVO weaponryVO, UserServiceCache.User user,
                                       RoleServiceCache.Role role, MessageVO messageVO) {
        /**
         * 判断装备是否已经使用，已经使用则不允许数值再次增加
         */
        if (!weaponryVO.getUsage().equals(BackpackLabel.WEAPONRY_UNUSED)) {
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_ERROR,
                    "装备已经使用！", new HashMap<>(), messageVO);
        } else if (!weaponryVO.getProfession().equals(role.getRoleVO().getProfession())) {
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_ERROR,
                    "当前角色职业不允许使用这件装备！", new HashMap<>(), messageVO);
        } else if (weaponryVO.getDurability() <= 0) {
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_ERROR,
                    "装备耐久度过低，装备无法使用！", new HashMap<>(), messageVO);
        } else {
            if (role.getPartToWeaponry().containsKey(weaponryVO.getPart())) {
                String useWeaponId = role.getPartToWeaponry().get(weaponryVO.getPart());
                WeaponryVO useWeaponryVO = weaponryServiceCache.getWeaponryServiceMap().get(useWeaponId).getWeaponryVO();
            }
            //装备被对应角色id装备
            weaponryVO.setUsage(role.getRoleVO().getAllRoleId());
            buffUtil.positive(weaponryVO.getNumberMap(), role.getRoleVO().getNumberMap());
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS,
                    "装备使用成功！", user.getWeaponryVOMap(), messageVO);
            List<RoleVO> roleVOList = new ArrayList<>();
            roleVOList.add(role.getRoleVO());
            UserWeaponryDO userWeaponryDO = new UserWeaponryDO(weaponryVO);
            userWeaponryDO.setUserId(user.getUserVO().getUserId());
            asynDBManager.distributeWork(user.getUserVO().getUserId(), () -> userWeaponryService.update(userWeaponryDO));
            routeUtils.sendAoiRoleBroadcastResponse(Common.GAME_ROLE_SUCCESS, sceneServiceCache
                            .getSceneServiceMap().get(user.getSceneId()).getUserIdSet(),
                    roleVOList);
            updateWeaponryBroadcast(CommonBackpack.UPDATE_WEAPONRY_USAGE_USE,
                    weaponryVO, user.getSceneId(), role.getRoleVO().getAllRoleId(), messageVO.getUserId());
        }
    }

    /**
     * 脱下装备
     *
     * @param weaponryVO
     * @param user
     * @param role
     * @param messageVO
     */
    @Override
    public void updateWeaponryUsageUnused(WeaponryVO weaponryVO, UserServiceCache.User user,
                                          RoleServiceCache.Role role, MessageVO messageVO) {
        /**
         * 判断装备是否已经使用，已经是未使用则不允许数值再次减少，返回使用错误
         */
        if (weaponryVO.getUsage() == BackpackLabel.WEAPONRY_UNUSED) {
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_ERROR,
                    "装备已经是未使用状态！", new HashMap<>(), messageVO);
        } else {
            weaponryVO.setUsage(BackpackLabel.WEAPONRY_UNUSED);
            buffUtil.negative(weaponryVO.getNumberMap(), role.getRoleVO().getNumberMap());
            UserWeaponryDO userWeaponryDO = new UserWeaponryDO(weaponryVO);
            userWeaponryDO.setUserId(user.getUserVO().getUserId());
            asynDBManager.distributeWork(user.getUserVO().getUserId(), () -> userWeaponryService.update(userWeaponryDO));
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS,
                    "装备脱下成功！", user.getWeaponryVOMap(), messageVO);
            List<RoleVO> roleVOList = new ArrayList<>();
            roleVOList.add(role.getRoleVO());
            routeUtils.sendAoiRoleBroadcastResponse(Common.GAME_ROLE_SUCCESS, sceneServiceCache
                            .getSceneServiceMap().get(user.getSceneId()).getUserIdSet(),
                    roleVOList);
            updateWeaponryBroadcast(CommonBackpack.UPDATE_WEAPONRY_USAGE_UNUSED,
                    weaponryVO, user.getSceneId(), role.getRoleVO().getAllRoleId(), messageVO.getUserId());
        }
    }

    /**
     * 广播给当前场景所有角色装备的变化情况
     *
     * @param code
     * @param weaponryVO
     * @param sceneId
     */
    public void updateWeaponryBroadcast(int code, WeaponryVO weaponryVO, String sceneId, String roleId, String userId) {
        Map<String, List<WeaponryVO>> userIdToWeaponryMap = new HashMap<>();
        List<WeaponryVO> weaponryVOList = new ArrayList<>();
        weaponryVOList.add(weaponryVO);
        userIdToWeaponryMap.put(userId, weaponryVOList);
        routeUtils.sendWeaponryBroadcastResponse(code,
                userIdToWeaponryMap, sceneServiceCache.getSceneServiceMap().get(sceneId).getUserIdSet(), roleId);
    }
}
